//
//  TYWaveProgressDemo.m
//  TYWaveProgressDemo
//
//  Created by tanyang on 15/4/14.
//  Copyright (c) 2015年 tanyang. All rights reserved.
//

#import "TYWaveProgressView.h"
#import "TYWaterWaveView.h"

@interface TYWaveProgressView ()
@property (nonatomic, weak,readwrite) UIImageView *backgroundImageView;
@property (nonatomic, weak) TYWaterWaveView *waterWaveView;

@end

@implementation TYWaveProgressView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setupView];
    }
    return self;
}

- (void)setupView
{
    [self addBackgroudImageView];
    [self addWaterWaveView];
    [self addNumberlabel];
    [self addBatteryIcon];
    [self addBatteryLabel];
}

- (void)addBackgroudImageView
{
    UIImageView *backgroundImageView = [[UIImageView alloc]init];
    [self addSubview:backgroundImageView];
    _backgroundImageView = backgroundImageView;
}

- (void)addWaterWaveView
{
    TYWaterWaveView *waterWaveView = [[TYWaterWaveView alloc]init];
    [self addSubview:waterWaveView];
    _waterWaveView = waterWaveView;
}

- (void)addNumberlabel
{
    UILabel *numberLabel = [[UILabel alloc]init];
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.font = [UIFont boldSystemFontOfSize:50];
    numberLabel.text = @"00:00";
    numberLabel.textColor = [UIColor whiteColor];
    [self addSubview:numberLabel];
    _numberLabel = numberLabel;
}

-(void)addBatteryIcon{
    UIImageView *icon = [[UIImageView alloc]init];
    icon.image = [UIImage imageNamed:@"0"];
    [self addSubview:icon];
    _batteryIcon = icon;
}

-(void)addBatteryLabel{
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:12];
    label.text = @"80%";
    label.textColor = [UIColor whiteColor];
    [self addSubview:label];
    _batteryLabel = label;
}
- (void)startWave
{
    if (_numberLabel.text) {
        if (_percent >= 0) {
            _waterWaveView.percent = _percent;
            [_waterWaveView startWave];
        }else{
            [self resetWave];
        }
    }
}

- (void)stopWave
{
    [_waterWaveView stopWave];
}

- (void)resetWave
{
    [_waterWaveView reset];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat viewHeight = CGRectGetHeight(self.frame);
    CGFloat viewWidth = CGRectGetWidth(self.frame);
    
    _backgroundImageView.frame = self.bounds;
    
    _waterWaveView.frame = CGRectMake(_waveViewMargin.left, _waveViewMargin.top, viewWidth-_waveViewMargin.left-_waveViewMargin.right, viewHeight - _waveViewMargin.top - _waveViewMargin.bottom);
    
    _waterWaveView.layer.cornerRadius = MIN(CGRectGetHeight(_waterWaveView.frame)/2, CGRectGetWidth(_waterWaveView.frame)/2);
    
    CGFloat numberLabelWidth = viewWidth * 2/3;
    CGFloat numberLabelHeight = _numberLabel.font.pointSize + 2;
    
    _numberLabel.frame = CGRectMake((viewWidth - numberLabelWidth)/2, (self.height-numberLabelHeight)/2-36, numberLabelWidth, numberLabelHeight);
    
    _batteryIcon.frame = CGRectMake((viewWidth - 16)/2, self.height - 30 - 10 -20, 16, 30);
    
    _batteryLabel.frame = CGRectMake(0, CGRectGetMaxY(_batteryIcon.frame)+5, self.width, 16);
}

- (void)dealloc{
    // 很重要 否则wave不能释放
    [self stopWave];
}

@end
